home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7377 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP!  cin.getline to cin
  5. Date: 23 Feb 1996 00:33:19 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4gj20f$ii0@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 21, 1996 18:12:48 in article <HELP!  cin.getline to cin>,
  15. 'scalish@execpc.com (Sherri Scalish)' wrote: 
  16.  
  17.  
  18. >I am a C++ student trying to use the cin.getline and cin stream  
  19. >operators to load in user input.  The data I am loading is first  
  20. >character strings, then floats, integers, then back to character  
  21. >strings.  Everything works fine with my cin.getlines until after I use  
  22. >the cin to load a integer or float, then nothing happens when I try to  
  23. >use the cin.getline function for the rest of the input.  Any ideas to  
  24. >share with me? 
  25. Yes.  After having read a value with cin >> somevar, the stream 
  26. is positioned at the newline which you had to enter to complete 
  27. your input.  If you now perform a getline(), it will "see" the newline 
  28. and return an empty string (well, not quite return.. but you get 
  29. what I mean).  You must first flush this out before you can get 
  30. the stuff on the next line. 
  31.  
  32. There may be more than one newline so, to be safe, do the 
  33. following: 
  34.  
  35.    int n; 
  36.    char buffer[80]; 
  37.    ... 
  38.    cin >> n; 
  39.    cout << "Enter a string: " << flush; 
  40.    do 
  41.       cin.getline(buffer, 80); 
  42.    while(! buffer[0]); 
  43.     .... process the next line 
  44.  
  45. -- 
  46. Pete Grant 
  47. Kalevi, Inc. 
  48. Software Engineering & development
  49.